package com.itheima.controller;

import com.itheima.dto.MealDto;
import com.itheima.pojo.Meal;
import com.itheima.result.R;
import com.itheima.service.MealService;
import com.itheima.vo.GetPageRes;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class MealController {
    @Resource
    private MealService mealService;

    @PostMapping
    public R<String> addMeal(@RequestBody MealDto dto) {
        boolean res = mealService.addMeal(dto);
        return res ? R.success("成功") : R.error("失败");
    }

    @GetMapping("/page")
    public R<GetPageRes<Meal>> getMealPage(@RequestParam(value = "name", required = false) String name,
                                           @RequestParam(value = "page", defaultValue = "1") int page,
                                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        List<Meal> mealPage = mealService.getMealPage(name, page, pageSize);
        GetPageRes<Meal> mealGetPageRes = new GetPageRes<>();
        mealGetPageRes.setRecords(mealPage);
        return R.success(mealGetPageRes);
    }

    @PutMapping
    public R<String> modifyMeal(@RequestBody Meal meal) {
        boolean res = mealService.modifyMeal(meal);
        return res ? R.success("成功") : R.error("失败");
    }

    @GetMapping("/{id}")
    public R<Meal> getMealById(@PathVariable("id") String id) {
        Meal meal = mealService.getMealById(id);
        return R.success(meal);
    }

    @PostMapping("/status/{status}")
    public R<String> modifyStatus(@PathVariable("status") Integer status, @RequestParam("ids") String ids) {
        String[] idsarr = ids.split(",");
        boolean res = mealService.modifyStatus(status, idsarr);
        return res ? R.success("成功") : R.error("失败");
    }

    @DeleteMapping
    public R<String> deleteDish(@RequestParam("ids") String ids) {
        String[] idarr = ids.split(",");
        boolean res = mealService.deleteMeal(idarr);
        return res ? R.success("成功") : R.error("失败");
    }
}
